package com.zy.mc_module.wheel.recyclerview.divider;

import android.graphics.Rect;
import androidx.recyclerview.widget.RecyclerView;
import android.view.View;

/**
 * Created on 2018/12/19
 * Recyclerview 水平等分分割线
 */
public class SpaceItemDivider extends RecyclerView.ItemDecoration {
    private int spanCount; //列数
    private int spacing; //间隔

    public SpaceItemDivider(int spanCount, int spacing) {
        this.spanCount = spanCount;
        this.spacing = spacing;
    }

    @Override
    public void getItemOffsets(Rect outRect, View child, RecyclerView parent, RecyclerView.State state) {
        super.getItemOffsets(outRect, child, parent, state);

        int pos = parent.getChildAdapterPosition(child);
        int column = (pos) % spanCount + 1;// 计算这个child 处于第几列
        //注意这里一定要先乘 后除  先除数因为小于1然后强转int后会为0
        outRect.left = (column - 1) * spacing / spanCount; //左侧为(当前条目数-1)/总条目数*divider宽度
        outRect.right = (spanCount - column) * spacing / spanCount;//右侧为(总条目数-当前条目数)/总条目数*divider宽度
    }
}
